package com.itheima.springboot_08_ssmp3.interceptor;

import cn.itcast.ip_spring_boot_starter.service.IpCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class IpCountInterceptor implements HandlerInterceptor { //这个是拦截器,用于在源代码前后执行,不同的重写方法注定了在前或后执行
    @Autowired
    private IpCountService ipCountService;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        ipCountService.count();
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }//在url触发之前执行

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        ipCountService.count();
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }//在url触发之后执行
}
